var ajax = require('../../utils/ajax.js');
var app = getApp();

Page({
    data: {
        dataLoaded: true,
        rewardList: [],
        userInfo: {},
        page: 1,
        loading: false,
        moreLoading: false,
        noMore: false,
        title: '',
        winHei:0
    },

    onLoad() {
        var _self = this;

        this._toTab();
    
        app.getUserInfo(function(userInfo) {
            _self.setData({
                userInfo
            });
        });
         wx.getSystemInfo({
            success: function(res) {
                _self.setData({
                    winHei:res.windowHeight
                })
            }
        })
    },
    onReachBottom: function(){
        this._loadMore();
    },

    _back() {
        wx.navigateBack();
    },

    _toTab: function(cb) {
        this.setData({
            rewardList: [],
            page: 1,
            noMore: false,
        });
        this._getSalesList({}, cb);
    },

    _getSalesList: function(params, cb) {
        var self = this;
        var isMore = params.isMore;
        var page = self.data.page || 1;
        var url = '/openapi/Shop/shopRewardList';

        if (isMore) {
            self.setData({
                moreLoading: true
            });
        } else {
            self.setData({
                loading: true
            });
        }
        ajax.G(url, {
            page: page,
            shopSid: app.getShopSid()
        }, function(data) {
            var list = data.result.list || [];
            var rewardList = self.data.rewardList;
            var toData = {
                rewardList: rewardList.concat(list),
                noMore: list.length == 0,
                page: self.data.page + 1,
                title: data.result.title
            };
            toData[isMore ? 'moreLoading' : 'loading'] = false;
            self.setData(toData);
            cb && cb();
        });
    },

    /**
     * 加载更多
     */
    _loadMore: function() {
        console.log('---_loadMore')
        if (this.data.moreLoading || this.data.loading || this.data.noMore) {
            return;
        }
        this._getSalesList({
            isMore: true
        });
    },
});